package com.wiken.storeservice.controller;


import com.sun.xml.internal.bind.v2.TODO;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.R;
import com.wiken.servicecommon.common.exception.DefaultException;
import com.wiken.storeservice.entity.Type;
import com.wiken.storeservice.service.impl.TypeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/storeservice/type/")
public class TypeController {

    @Autowired
    private TypeServiceImpl typeService;

    @PostMapping("/addType")
    public R addType(@RequestBody Type type, @RequestHeader("Authorization") String token){
        boolean ok = typeService.addType(type, token);

        if(!ok) throw new DefaultException(Code.ERROR, "添加类型失败");

        return R.ok().code(Code.SUCCESS).message("添加成功");
    }

    @GetMapping("/getType")
    public R getType(){
        List<Type> typeList = typeService.getTypes();

        return R.ok().code(Code.SUCCESS).data("typeList", typeList);
    }

    @PutMapping("/updateType")
    public R updateType(@RequestBody Type type, @RequestHeader("Authorization") String token){
        boolean ok = typeService.updateType(type, token);

        if(!ok) throw new DefaultException(Code.ERROR, "修改类型失败");

        return R.ok().code(Code.SUCCESS).message("修改类型成功");
    }

    @DeleteMapping("/deleteType")
    public R deleteType(@RequestBody Type type, @RequestHeader("Authorization") String token){

        boolean ok = typeService.deleteType(type, token);

        if(!ok) throw new DefaultException(Code.ERROR, "删除类型失败");
        return R.ok().code(Code.SUCCESS).message("删除类型成功");
    }
}
